<template>
  <el-dialog
    :title="!dataForm.developersId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="开发商名称" prop="developersName">
      <el-input v-model="dataForm.developersName" placeholder="开发商名称"></el-input>
    </el-form-item>
    <el-form-item label="开发商排序" prop="developersSort">
      <el-input v-model="dataForm.developersSort" placeholder="开发商排序"></el-input>
    </el-form-item>
    <el-form-item label="开发商状态" prop="developersStatic">
      <el-input v-model="dataForm.developersStatic" placeholder="开发商状态"></el-input>
    </el-form-item>
    <el-form-item label="开发商省份" prop="developersProvince">
      <el-input v-model="dataForm.developersProvince" placeholder="开发商省份"></el-input>
    </el-form-item>
    <el-form-item label="开发商城市" prop="developersCity">
      <el-input v-model="dataForm.developersCity" placeholder="开发商城市"></el-input>
    </el-form-item>
    <el-form-item label="开发商县区" prop="developersCounty">
      <el-input v-model="dataForm.developersCounty" placeholder="开发商县区"></el-input>
    </el-form-item>
    <el-form-item label="开发商手机" prop="developersPhone">
      <el-input v-model="dataForm.developersPhone" placeholder="开发商手机"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          developersId: 0,
          developersName: '',
          developersSort: '',
          developersStatic: '',
          developersProvince: '',
          developersCity: '',
          developersCounty: '',
          developersPhone: ''
        },
        dataRule: {
          developersName: [
            { required: true, message: '开发商名称不能为空', trigger: 'blur' }
          ],
          developersSort: [
            { required: true, message: '开发商排序不能为空', trigger: 'blur' }
          ],
          developersStatic: [
            { required: true, message: '开发商状态不能为空', trigger: 'blur' }
          ],
          developersProvince: [
            { required: true, message: '开发商省份不能为空', trigger: 'blur' }
          ],
          developersCity: [
            { required: true, message: '开发商城市不能为空', trigger: 'blur' }
          ],
          developersCounty: [
            { required: true, message: '开发商县区不能为空', trigger: 'blur' }
          ],
          developersPhone: [
            { required: true, message: '开发商手机不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.developersId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.developersId) {
            this.$http({
              url: this.$http.adornUrl(`/backstage/developers/info/${this.dataForm.developersId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.developersName = data.developers.developersName
                this.dataForm.developersSort = data.developers.developersSort
                this.dataForm.developersStatic = data.developers.developersStatic
                this.dataForm.developersProvince = data.developers.developersProvince
                this.dataForm.developersCity = data.developers.developersCity
                this.dataForm.developersCounty = data.developers.developersCounty
                this.dataForm.developersPhone = data.developers.developersPhone
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/backstage/developers/${!this.dataForm.developersId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'developersId': this.dataForm.developersId || undefined,
                'developersName': this.dataForm.developersName,
                'developersSort': this.dataForm.developersSort,
                'developersStatic': this.dataForm.developersStatic,
                'developersProvince': this.dataForm.developersProvince,
                'developersCity': this.dataForm.developersCity,
                'developersCounty': this.dataForm.developersCounty,
                'developersPhone': this.dataForm.developersPhone
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
